<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Teachings extends Backend_Controller {

	protected $_title = 'Maintenance Data Guru';

	public function __construct() {
		parent::__construct();
		$this->load->model('subjects_model','subjects');
		$this->load->model('teachers_model','teachers');
		$this->load->model('classes_model','classes');
		$this->load->model('teachings_model','teachings');
		
		if(parent::get_actor_login()!="admin")
			redirect('login/index/1');
	}

	private function _generate_pagination($uri_segment,$limit,$condition){
		$rows = $this->teachers->_count($condition);
		$config['base_url'] = site_url('admin/teachings/index');
		$config['total_rows'] = $rows;
		$config['per_page'] = $limit;
		$config['uri_segment'] = $uri_segment;
		$this->pagination->initialize($config);
		return $this->pagination->create_links();
	}
	
	public function index($offset=0, $sort_by='name') {
		$keyword = $this->input->post('keyword');
		$condition = '';
		if(!empty($keyword)){
			$condition = $this->input->post('search_by') . " LIKE '%" . $keyword . "%'";
		}
		
		$limit=20;
		$uri_segment = 4;
		$teachers = $this->teachers->lists($condition, $sort_by, $limit, $offset);
		$pagin = $this->_generate_pagination($uri_segment,$limit,$condition);
		$total_rows = $this->teachers->_count($condition);
		
		$data = array(
			'mainview' => 'admin/teachings/index',
			'school_year_id' => $this->_school_year_id,
			'school_year' => $this->_school_year,
			'semester' => $this->_semester,
			'info' => $this->session->flashdata('info'),
			'error' => $this->session->flashdata('error'),
			'teachers' => $teachers,
			'offset'=> $offset,
			'pagination'=> $pagin,
			'total_rows'=> $total_rows,
			'search_by' => $this->input->post('search_by')
		) + $this->_get_header_data();

		$this->load->view('admin/template', $data);
	}

	public function add($teacher_id='',$subject_id='') {
		$data = array(
			'mainview' => 'admin/teachings/add',
			'school_year_id' => $this->_school_year_id,
			'school_year' => $this->_school_year,
			'semester' => $this->_semester,
			'teacher' => $this->teachers->get($teacher_id),
			'subject_id'=> $subject_id,
			'links' => array('link_back' => back_link('admin/teachings', 'Kembali'))
		) + $this->_get_header_data();

		$insert_subject = $this->input->post('insert_subject');
		if (!empty($insert_subject)) {
			$this->teachings->insert($this->input,'subject');
			$teacher = $this->teachers->get($this->input->post('teacher_id'));
			$this->session->set_flashdata('info', "Data Bidang Studi Guru ({$teacher['name']}) berhasil ditambahkan.");
			redirect('admin/teachings');			
		}
		
		$insert_class = $this->input->post('insert_class');
		if (!empty($insert_class)) {
			$this->teachings->insert($this->input,'class');
			$teacher = $this->teachers->get($this->input->post('teacher_id'));
			$this->session->set_flashdata('info', "Data Bidang Studi Guru ({$teacher['name']}) berhasil ditambahkan.");
			redirect('admin/teachings');			
		}
		$this->load->view('admin/template', $data);
	}
}
